package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.DeleteMapping;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //判断购物车里是否已经存在该商品
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //获取用户ID
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);

        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        //如果存在，则数量加一
        if(list != null && list.size() > 0){
            ShoppingCart cart = list.get(0);
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartMapper.updateNumberById(cart);
        }else{
            //如果不存在，则新增一条数据
            //判断添加的商品是套餐还是菜品
            Long dishId = shoppingCartDTO.getDishId();
            if(dishId != null){
                //本次添加的是菜品,将购物车商品未赋值的属性赋值
                Dish dish = dishMapper.getById(dishId);
                //名称
                shoppingCart.setName(dish.getName());

                //金额，单价
                shoppingCart.setAmount(dish.getPrice());

                //图片
                shoppingCart.setImage(dish.getImage());

            }else{
                //本次添加的是套餐
                //获取套餐Id
                Long setmealId = shoppingCartDTO.getSetmealId();
                Setmeal setMeal = setmealMapper.getById(setmealId);
                //名称
                shoppingCart.setName(setMeal.getName());

                //金额，单价
                shoppingCart.setAmount(setMeal.getPrice());

                //图片
                shoppingCart.setImage(setMeal.getImage());

            }
            //创建时间
            shoppingCart.setCreateTime(LocalDateTime.now());
            //数量，初始数量为1
            shoppingCart.setNumber(1);
            shoppingCartMapper.insert(shoppingCart);
        }

    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> showShoppingCart() {
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(userId)
                .build();
       List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        return list;
    }

    /**
     * 清除购物车
     */
    @Override
    public void delete() {

        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.cleanShopCartById(userId);
    }


}
